home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / util / Mac F2C 1.3.sit / Mac F2C 1.3 / Mac F2C v1.3 Documentation.rsrc / TEXT_141.txt < prev    next >
Text File  |  1995-12-11  |  6KB  |  120 lines

  1. When Translated Code Won‚Äôt Run
  2.  
  3.  
  4. It is not unusual for code produced by Mac F2C to compile without any problems, but crash and burn when run.  In fact, if you get to the ccommand() dialog (the one that asks you to set up standard input and standard output), but crash immediately there after, it is almost certain that you are blowing out the stack space.  The problem and its solution are described below.
  5.  
  6. The problem arises because FORTRAN allocates memory for all its variables statically.  The C code produced by Mac F2C by default uses automatic storage for most variables.  On the Macintosh automatic variables get stored on the stack.  When the FORTRAN code declares large arrays (quite common in FORTRAN code), these become large arrays on the stack in the C version.  Because less than 24K is normally allocated as stack space by default on the Macintosh, your program blows out the stack.
  7.  
  8. The quick fix to try is to translate the FORTRAN code with the Make local variables automatic vice static option in the C Output options dialog unchecked.  This will move all of the large arrays from the stack to the global data space.  You should also increase the memory partition for your program (either in the Finder‚Äôs Get Info dialog if it is a free-standing application, or in the appropriate THINK, Symantec, or CodeWarrior dialog if it is under development) to allow for the extra memory required by the large global arrays.
  9.  
  10. This ‚Äúfix‚Äù will often solve the problem.  However, because THINK still imposes a 32K limit on the amount of global data per file, you can still have problems with files that have too many large arrays in them.  If you are lucky, you can divide the file somehow so that the arrays are split among several files and now don‚Äôt violate the 32K/file limit.  However, that might not be possible if, for example, all the arrays appear within a single subroutine.
  11.  
  12. In such a case, the only solution is to edit the C code so that it allocates the memory dynamically.
  13.  
  14. Let‚Äôs work an example.  Consider the following simple FORTRAN program:
  15.  
  16.     program example
  17.     
  18.     double precision d
  19.     dimension a(100,100), b(100,200,20)
  20.     dimension d(50,20,10)
  21.     
  22.     a(100,100) = 1.0
  23.     b(100,200,20) = 1.0
  24.     d(50,20,10) = 1.0d0
  25.     
  26.     stop
  27.     end
  28.  
  29. Basically all it does is allocate three arrays.  Note that the arrays are not that large by FORTRAN standards.  Selecting the option that allocates local variables automatically, this program translates into:
  30.  
  31. /* JUNK.f -- translated by f2c (version 19941113).
  32.    You must link the resulting object file with the libraries:
  33.     -lf2c -lm   (in that order)
  34. */
  35.  
  36. #include "f2c.h"
  37.  
  38. /* Main program */ MAIN__(void)
  39. {
  40.     /* Builtin functions */
  41.     /* Subroutine */ int s_stop(char *, ftnlen);
  42.  
  43.     /* Local variables */
  44.     real a[10000]   /* was [100][100] */, b[400000] /* was [100][200][20] 
  45.         */;
  46.     doublereal d[10000] /* was [50][20][10] */;
  47.  
  48.     a[9999] = 1.f;
  49.     b[399999] = 1.f;
  50.     d[9999] = 1.;
  51.     s_stop("", 0L);
  52.     return 0;
  53. } /* MAIN__ */
  54.  
  55. /* Main program alias */ int example_ () { MAIN__ (); return 0; }
  56.  
  57.  
  58. Note how in the C version 410,000 reals and 10,000 doublereals are allocated on the stack‚Äîthat‚Äôs over 1.5MB on the stack.  Your program will crash the moment it enters the MAIN__() function.  If you tried using static vice automatic variables you would have over 32K of global data in one file and it wouldn‚Äôt build under THINK C.  
  59.  
  60. Multi-dimensional arrays can be quite large even when their individual dimensions are small.  It‚Äôs easy to blow out the stack or the THINK C limit on global data per file with even one or two arrays of three or more dimensions.  I‚Äôve also seen programs get into problems because they have lots of small arrays.
  61.  
  62. The solution is to replace the array variables with pointers and allocate the memory for them using malloc() or any of it‚Äôs cousins.  Because C treats pointers and arrays similarly, the rest of the code works as is. 
  63.  
  64. Applying this technique to the example yields the following modified code:
  65.  
  66. /* JUNK.f -- translated by f2c (version 19941113).
  67.    You must link the resulting object file with the libraries:
  68.     -lf2c -lm   (in that order)
  69. */
  70.  
  71. #include "f2c.h"
  72.  
  73. #define NEW_CODE            /* <========  to make the changes clear */
  74.  
  75. #ifdef NEW_CODE
  76. #include <stdlib.h>         /* Get prototypes for malloc(), etc */
  77. #endif
  78.  
  79. /* Main program */ MAIN__(void)
  80. {
  81.     /* Builtin functions */
  82.     /* Subroutine */ int s_stop(char *, ftnlen);
  83.  
  84.     /* Local variables */
  85. #ifdef NEW_CODE 
  86.     real        *a, *b;         /* Pointers instead of arrays */
  87.     doublereal  *d;         
  88. #else
  89.     real a[10000]   /* was [100][100] */, b[400000] /* was [100][200][20] 
  90.         */;
  91.     doublereal d[10000] /* was [50][20][10] */;
  92. #endif
  93.  
  94.  
  95. #ifdef NEW_CODE                                 /* allocate the memory */
  96.     a = (real *) malloc( 10000*sizeof(real) );
  97.     b = (real *) malloc( 400000*sizeof(real) );
  98.     d = (doublereal *) malloc( 10000*sizeof(doublereal) );
  99.     /* Remember to check a, b, and d for NULL (e.g., out of memory) */
  100. #endif
  101.  
  102.     a[9999] = 1.f;
  103.     b[399999] = 1.f;
  104.     d[9999] = 1.;
  105.     
  106. #ifdef NEW_CODE
  107.     free( a );          /* Don't forget to free the memory */
  108.     free( b );
  109.     free( d );
  110. #endif
  111.  
  112.     s_stop("", 0L);
  113.     return 0;
  114. } /* MAIN__ */
  115.  
  116. /* Main program alias */ int example_ () { MAIN__ (); return 0; }
  117.  
  118. This version of the program will work fine so long as there is enough heap space to allocate the memory requested in the malloc() calls.  You can add more heap space by simply increasing the partition size of the program (either in the Finder‚Äôs Get Info dialog if it is a free-standing application, or in the appropriate THINK, Symantec, or CodeWarrior dialog if the code is under development).
  119.  
  120. This modified version will also work if the variables a, b, and d had been declared static vice automatic.